#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#include<errno.h>
void Creat_ID();//生成ID，并且判断合法性

void Function_jump();//功能跳转

void menu();//打印菜单

void Test();//开始测试

void RAND(int* p);//生成随机数

void RAND_opear(char* opear);

void Check();//查看分数

void RAND_answer(int* num1,char* opear,int* num2,int* answer);//生成答案

void Answer_Juge(int* num1,char opear,int* num2,int* answer);//判断答案合理性，并且修改

void store(int tol_time,int score);//存储成绩

void Provide_ans(int* num1,char* opear,int* num2,int* answer,int* U_answer);//公布答案

char ID[7],buf[1024];
int main()
{
	Creat_ID();//生成ID，并且判断合法性
	Function_jump();//功能跳转	
	return 0;
}

void Creat_ID()
{
	int flag=1,i=0;//当flag=0时，也就是ID合法，停止循环
	while(flag){
		int count=0;	
		printf("请输入ID，ID格式为：前两位大写字母，后四位为数字\n如：WA1234\n");
		while((buf[i++]=getchar())!='\n'){
			count++;
		}if(count>6){
			printf("请输入6位ID\n");
			continue;
		}else{
			for(i=0;i<6;i++)
				ID[i]=buf[i];
		}
		if((ID[0]<'A' || ID[0]>'Z')||(ID[1]<'A'|| ID[1]>'Z')){//1,2号位置的字符不是大写字母
			printf("前两位出错\n");
			continue;
		}
		for(i=2;i<6;i++){
			if(ID[i]<'0' ||ID[i]>'9'){
				printf("后四位出错\n");
				break;
			}
		}
		if(i==6){
			printf("创建成功\n");
			flag=0;//推出循环
		}
	}	
}

void Function_jump()
{	
	int juge,choice;
	printf("欢迎来到小学数学题测试系统，现在位于功能选择界面\n");
	do{
input:	menu();//打印菜单
	scanf("%d",&choice);
	if(choice==3)//如果选择3，直接退出循环
		break;
	switch(choice){
		case 1:Test();break;
		case 2:Check();break;
		default:printf("请正确输入\n");goto input;
	}
loop: 	printf("是否重新选择功能？是，请输入1，不是则输入0\n");//询问是否重新选择功能
	scanf("%d",&juge);
	if(juge!=1 && juge!=0){
		printf("请输入正确的数字\n");
		goto loop;//重新输入
	}
	}while(juge);
}

void menu()
{
	printf("请输入要选择的功能\n");
	printf("1:开始测试\n2：查询历史成绩\n3：退出\n");
}

void Test()
{
	int flag=1;
	do{
		int num1[10],num2[10],answer[10],U_answer[10],score=0,tol_time=0;
		char opear[10];
		srand((unsigned int)time(NULL));
		RAND(num1);
		RAND(num2);
		RAND_opear(opear);
		RAND_answer(num1,opear,num2,answer);
		int start=time(NULL);
		for(int i=0;i<10;i++){//答题
			printf("%d%c%d=",num1[i],opear[i],num2[i]);
			scanf("%d",&U_answer[i]);
			if(answer[i]==U_answer[i])
				score+=10;
		}int end=time(NULL);
		tol_time=end-start;
		store(tol_time,score);//存储信息
		Provide_ans(num1,opear,num2,answer,U_answer);//公布答案
		printf("是否继续测试？\n1：是 0：不是\n");scanf("%d",&flag);
	}while(flag);
}

void RAND(int* p)//生成随机数
{
	int i=0;
	for(i=0;i<10;i++){
		p[i]=rand()%30+1;
	}
	for(i=0;i<10;i++){
		for(int j=i+1;j<10;j++){
			if(p[j]==p[i]){
				p[j]=rand()%30+1;
				for(int k=j-1;k>=0;k--){
					if(p[j]==p[k]){
						p[j]=rand()%30+1;
						k=j;
					}
				}
			}
		}
	}	

}

void RAND_opear(char* opear)
{
	int last_index=0,index=0;
	struct opear_four{
		char opear[4];
		int count[4];
	};
	struct opear_four opearation={{'+','-','*','/'},{0}};
	for(int i=0;i<10;i++){
		while(index==last_index){
			index=rand()%4;
		}
		if(i>=8){
			if(opearation.count[0]==0){
				opearation.count[0]+=1;
			}else if(opearation.count[1]==0){
				opearation.count[1]+=1;
			}else if(opearation.count[2]==0){
				opearation.count[2]+=1;
			}else if(opearation.count[3]==0){
				opearation.count[3]+=1;
			}
		}
		opear[i]=opearation.opear[index];
		opearation.count[index]+=1;
		last_index=index;
	}
}

void RAND_answer(int* num1,char* opear,int* num2,int* answer)
{
	for(int i=0;i<10;i++){
		switch(opear[i]){
			case '+':answer[i]=num1[i]+num2[i];Answer_Juge(&num1[i],'+',&num2[i],&answer[i]);break;
			case '-':answer[i]=num1[i]-num2[i];Answer_Juge(&num1[i],'-',&num2[i],&answer[i]);break;
			case '*':answer[i]=num1[i]*num2[i];Answer_Juge(&num1[i],'*',&num2[i],&answer[i]);break;
			case '/':answer[i]=num1[i]/num2[i];Answer_Juge(&num1[i],'/',&num2[i],&answer[i]);break;
		}
	}
}

void Answer_Juge(int* num1,char opear,int* num2,int* answer)
{
	switch(opear){
		case '+':break;
		case '-':
			 if(*answer<=0){
				 int tmp=0;
				 tmp=*num1;*num1=*num2+1;*num2=tmp;*answer=*num1-*num2;
			 }break;
		case '*':if(*answer>=100){
				 *num1=*num1/3+1;
				 *num2=*num2/3+1;*answer=(*num1)*(*num2);
			 }break;
		case '/':*num2=rand()%5+1;int k=rand()%15+1;*num1=k*(*num2);*answer=k;break;
	}
}


void store(int tol_time,int score)
{
	FILE* fp=fopen("record.txt","a+");
	fprintf(fp,"ID:%s,时间:%d秒,分数:%d分\n",ID,tol_time,score);
	fclose(fp);
}


void Provide_ans(int* num1,char* opear,int* num2,int* answer,int* U_answer)
{
	for(int i=0;i<10;i++){
		printf("%d%c%d=%d,你的答案是：%d\n",num1[i],opear[i],num2[i],answer[i],U_answer[i]);

	}
}

void Check()
{
	char buf1[6];
	int time,score,ret;
	FILE* fp=fopen("record.txt","r");
	do{		
		ret=fscanf(fp,"ID:%[^,],时间:%d秒,分数:%d分\n",buf1,&time,&score);
		if(strcmp(buf1,ID)==0){
			printf("ID:%s,时间：%d秒,分数:%d分\n",buf1,time,score);
		}
	}while(ret!=EOF);
	fclose(fp);
}
